def merge_dictionaries(dict1, dict2):
    '''
        合并两个字典，当key重复时后者覆盖前者value
    :param dict1:
    :param dict2:
    :return: 合并后的字典
    '''
    return merge_dictionaries_advanced(dict1, dict2, 'last')


def choose(handler, old, new):
    if handler == 'first':
        return old
    elif handler == 'last':
        return new
    elif handler == 'error':
        raise KeyError("相同key已经存在")
    pass


def merge_dictionaries_advanced(dict1, dict2, handler):
    '''
        合并两个字典
    :param dict1: 第一个字典
    :param dict2: 第二个字典
    :param handler: 当key重复时处理方案，取值有 first｜last｜error
    :return 合并后的字典:
    '''
    if not isinstance(dict1, dict) or not isinstance(dict2, dict):
        raise TypeError("两个参数都必须是字典类型")
    dict3 = dict1.copy()
    for k2, v2 in dict2.items():
        if dict3.get(k2) is not None:
            dict3[k2] = choose(handler, dict3.get(k2), v2)
        else:
            dict3[k2] = v2
    return dict3


s1 = {
    "k1": "v1",
    "k2": "v2",
    "k3": "v3",
    "k4": "v4"
}

s2 = {
    "k1": "vv1",
    "kk2": "vv2",
    "kk3": "vv3",
    "k4": "vv4"
}

s4 = merge_dictionaries(s1, s2)
s5 = merge_dictionaries_advanced(s1, s4, 'first')
s6 = merge_dictionaries_advanced(s1, s4, 'last')
print(s1)
print(s2)
print(s4)
print(s5)
print(s6)
sa = {'张三', '王五', '马六'}
sb = {'张三', 'disc'}
# 集合求交集
print(sa & sb)

# 集合求并集
print(sa | sb)

# 集合求差集
print(sa - sb)
